library(tidyverse)
library(ggplot2)
library(readxl)
library(readr)
::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE) knitr
Challenge 7
Challenge Overview
Today’s challenge is to:
- read in a data set, and describe the data set using both words and any supporting information (e.g., tables, etc)
- tidy data (as needed, including sanity checks)
- mutate variables as needed (including sanity checks)
- Recreate at least two graphs from previous exercises, but introduce at least one additional dimension that you omitted before using ggplot functionality (color, shape, line, facet, etc) The goal is not to create unneeded chart ink (Tufte), but to concisely capture variation in additional dimensions that were collapsed in your earlier 2 or 3 dimensional graphs.
- Explain why you choose the specific graph type
- If you haven’t tried in previous weeks, work this week to make your graphs “publication” ready with titles, captions, and pretty axis labels and other viewer-friendly features
R Graph Gallery is a good starting point for thinking about what information is conveyed in standard graph types, and includes example R code. And anyone not familiar with Edward Tufte should check out his fantastic books and courses on data visualizaton.
(be sure to only include the category tags for the data you use!)
Read in data
Read in one (or more) of the following datasets, using the correct R package and command.
- eggs ⭐
- abc_poll ⭐⭐
- australian_marriage ⭐⭐
- hotel_bookings ⭐⭐⭐
- air_bnb ⭐⭐⭐
- us_hh ⭐⭐⭐⭐
- faostat ⭐⭐⭐⭐⭐
<- read_csv("~/Github/601_Fall_2022/posts/_data/australian_marriage_tidy.csv") australian_marriage_tidy
Error: '~/Github/601_Fall_2022/posts/_data/australian_marriage_tidy.csv' does not exist.
<- australian_marriage_law_postal_survey_2017_response_final <- read_excel("~/Github/601_Fall_2022/posts/_data/australian_marriage_law_postal_survey_2017_-_response_final.xls",
table2 sheet = "Table 1", col_names = FALSE,
skip = 4)
Error: `path` does not exist: '~/Github/601_Fall_2022/posts/_data/australian_marriage_law_postal_survey_2017_-_response_final.xls'
<- australian_marriage_tidy df
Error in eval(expr, envir, enclos): object 'australian_marriage_tidy' not found
Briefly describe the data
The Australian marriage data set represents data gathered from a November 2017 postal survey of Australian public opinion on the legality of same-sex marriage. The survey was administered to registered voters across all 150 Federal Electoral Divisions in Australian states/territories . Respondents received one question to which an answer was either yes, no or response not clear.
Tidy Data (as needed)
As seen below, the data is tidy and only contains 16 observations of 4 variables (16 x 4).
summary(df)
Error in object[[i]]: object of type 'closure' is not subsettable
Are there any variables that require mutation to be usable in your analysis stream? For example, do you need to calculate new values in order to graph them? Can string values be represented numerically? Do you need to turn any variables into factors and reorder for ease of graphics and visualization?
Document your work here.
#Pivoting responses to be independent variables
<- df %>%
df pivot_wider(names_from = resp, values_from = c(percent,count)) %>%
mutate(`Total responses`= (`count_yes` + `count_no`))
Error in UseMethod("pivot_wider"): no applicable method for 'pivot_wider' applied to an object of class "function"
#Recoding names of the Australian Territories
$territory <- recode(df$territory, `New South Wales`= "NSW", `Northern Territory(b)` = "N. Ter(b)", `Australian Capital Territory(c)`= "Cap. Ter(c)", `Western Australia`= "W. Aus", `South Australia`= "S. Aus", `Victoria`= "Vict.", `Queensland`= "Qnld", `Tasmania`= "Tas") df
Error in df$territory: object of type 'closure' is not subsettable
print(df)
function (x, df1, df2, ncp, log = FALSE)
{
if (missing(ncp))
.Call(C_df, x, df1, df2, log)
else .Call(C_dnf, x, df1, df2, ncp, log)
}
<bytecode: 0x000001f0658c97f0>
<environment: namespace:stats>
Visualization with Multiple Dimensions
#Respondents to postal survey by province
ggplot(df, aes(x =reorder(territory, -`Total responses`), y = `Total responses`)) +
geom_bar(stat = "identity", fill= "dark blue") +
labs(x= " Austrailian Territory", y= "Total Respondents" )+
ggtitle("Graph 1.1: Respondents by Territory")
Error in `ggplot()`:
! `data` cannot be a function.
ℹ Have you misspelled the `data` argument in `ggplot()`
#Respondents Not in Favor of Same-Sex Marriage
ggplot(df, aes(x =reorder(territory, -`percent_no`), y =`count_no`)) +
geom_bar(stat = "identity", fill= "dark red") +
labs(x= " Austrailian Territory", y= "Number of Respondents" )+
ggtitle("Graph 1.2: Respondents Not in Favour of Same-Sex Marriage")
Error in `ggplot()`:
! `data` cannot be a function.
ℹ Have you misspelled the `data` argument in `ggplot()`
#Co-relation between Approval percentage and Number of respondents
ggplot(df, aes(x =`percent_no`, y= `Total responses`, colour=territory )) +
geom_point() +
geom_smooth(color= 'blue',method='lm', formula= y~x)+
labs(x= " Disapproval %", y= "Number of Respondents", colour= "Territory" )+
ggtitle("Graph 1.3: Total Respondents vs Disapproval %")
Error in `ggplot()`:
! `data` cannot be a function.
ℹ Have you misspelled the `data` argument in `ggplot()`
#Recoding names of the Australian Territories
$territory <- recode(australian_marriage_tidy$territory, `New South Wales`= "NSW", `Northern Territory(b)` = "N. Ter", `Australian Capital Territory(c)`= "Cap. Ter", `Western Australia`= "W. Aus", `South Australia`= "S. Aus", `Victoria`= "Vict.", `Queensland`= "Qnld", `Tasmania`= "Tas") australian_marriage_tidy
Error in recode(australian_marriage_tidy$territory, `New South Wales` = "NSW", : object 'australian_marriage_tidy' not found
#Approval/ Disapproval of same-sex marriage by Australian Territory
%>%
australian_marriage_tidy ggplot() + aes(x=territory, y= percent, fill= resp)+
geom_bar(stat="identity", color= "blue")+
facet_wrap(vars(resp))+
ggtitle("Graph 1.4: Approval % by Australian Territory")+
labs(y= "Percentage", x= "Australian Territory", fill= "Response")
Error in ggplot(.): object 'australian_marriage_tidy' not found